package com.joney.website.service;

import com.alibaba.fastjson.JSON;
import com.joney.common.exception.DefaultException;
import com.joney.common.pojo.Result;
import com.joney.common.pojo.UserSession;
import com.joney.common.utils.DateFormatType;
import com.joney.common.utils.DateUtil;
import com.joney.common.utils.JwtToken;
import com.joney.common.utils.MD5Util;
import com.joney.website.dto.request.ConsumeAdd;
import com.joney.website.dto.request.ConsumeQry;
import com.joney.website.dto.request.Login;
import com.joney.website.dto.request.Register;
import com.joney.website.entity.Consume;
import com.joney.website.entity.User;
import com.joney.website.entity.enums.ConsumeKindsEnum;
import com.joney.website.entity.enums.UserStateEnum;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author wangxianchen
 * @create 2018-11-13
 * @desc
 */
@Service
public class BizService extends BaseService {


    public Result login(Login req){
        User user = userService.findByLoginName(req.getLoginName());
        if(user == null){
            throw new DefaultException("用户不存在");
        }
        if(!MD5Util.encode(req.getPassword()).equals(user.getPassword())){
            throw new DefaultException("用户名或密码不对");
        }
        if(user.getState() == UserStateEnum.DISABLE.code()){
            throw new DefaultException("帐户已禁用，请联系管理员");
        }

        UserSession session = new UserSession();
        session.setUid(user.getId());
        session.setUname(user.getLoginName());
        String token;
        try {
            token = JwtToken.sign(JSON.toJSONString(session));
        } catch (Exception e) {
            throw new DefaultException("系统异常");
        }

        return Result.success(token);

    }
    public Result register(Register req){
        User user = userService.findByLoginName(req.getLoginName());
        if(user != null){
            throw new DefaultException("用户名已存在，请换一个试试");
        }
        user = new User();
        user.setLoginName(req.getLoginName());
        user.setPassword(MD5Util.encode(req.getPassword()));
        user.setSex(req.getSex());
        user.setState(UserStateEnum.ENABLE.code());
        user.setBirthday(DateUtil.convertDate(req.getBirthday()));
        userService.insert(user);
        return Result.success();
    }

    public Result consumeSave(ConsumeAdd req){
        Consume temp = new Consume();
        temp.setUid(req.getUid());
        temp.setCost(new BigDecimal(req.getCost()));
        temp.setKinds(ConsumeKindsEnum.getKindsEnumByCode(req.getKinds()));
        temp.setDate(DateUtil.convertDate(req.getDate()));
        temp.setAddr(req.getAddr());
        temp.setRemark(req.getRemark());
        consumeService.insert(temp);
        return Result.success();
    }

    public List<Consume> consumeList(ConsumeQry req){
        return consumeService.consumeList(req);
    }
    public Map<String,Object> totalInfo(List<Consume> list){
        Map<String,Object> map = new HashMap<>();
        Set<Date> dates = new HashSet<>();
        Set<Integer> kinds = new HashSet<>();
        BigDecimal total = new BigDecimal(0);
        if(list != null && list.size() > 0){
            for(Consume consume : list){
                total = total.add(consume.getCost());
                dates.add(consume.getDate());
                kinds.add(consume.getKinds().code());
            }


        }
        map.put("totalCost",total.doubleValue());
        map.put("totalKinds","包含 "+kinds.size()+" 个种类");
        map.put("totalDays","累计 "+dates.size()+" 天");
        return map;
    }

    public Map<String,Object> totalCurMonthCost(long uid){
        Map<String,Object> map = new HashMap<>();
        ConsumeQry req = new ConsumeQry();
        req.setUid(uid);
        List<Consume> list = consumeService.consumeSumCost(req);
        BigDecimal total = new BigDecimal(0);
        if(list != null && list.size() > 0){
            for(Consume consume : list){
                total = total.add(consume.getCost());
            }

        }
        map.put("totalCost",total.doubleValue());
        map.put("list",list == null ? new ArrayList<>() : list);
        map.put("curMonth",DateUtil.formatDateToString(new Date(), DateFormatType.YYYY_MM.getValue()));
        return map;
    }


}
